package com.hzit.controller;

import com.hzit.entity.PageResult;
import com.hzit.entity.R;
import com.hzit.entity.Student;
import com.hzit.service.StudentService;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@RestController
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private StudentService studentService;

	//1. 查询学生列表
	@GetMapping("/list")
	public R findAll(){
		List<Student> list = studentService.list();
		return R.ok().data("list",list);
	}
	//2. 条件查询带分页
	@PostMapping("/search/{page}/{pageSize}")
	public R search(@PathVariable("page") int page,
	                @PathVariable("pageSize") int pageSize,
	                @RequestBody Student student){
		PageResult<Student> pr = studentService.search(page,pageSize,student);
		return R.ok().data("pr",pr);
	}

	//3. 添加学生
	@PostMapping("/add")
	public R add(@RequestBody Student student){
		studentService.save(student);
		return R.ok().message("添加学生成功！");
	}

	//4. 修改学生
	@PostMapping("/update")
	public R update(@RequestBody Student student){
		studentService.updateById(student);
		return R.ok().message("修改学生成功！");
	}

	//5. 删除学生 (同时处理单个删除及批量删除)
	@GetMapping("/delete/{sid}")
	public R delete(@PathVariable("sid") String sid){  //1,2,3
		String[] split = sid.split(",");
		studentService.removeByIds(Arrays.asList(split));
		return R.ok().message("删除学生成功！");
	}

	// public static void main(String[] args) {
	// 	String list = "1,2,3";
	// 	List<String> strings = Arrays.asList(list);
	// 	System.out.println("strings = " + strings);
	// }
}
